﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.Text;
using QualityRpt.Service;

namespace QualityRPT
{
    public class AppTools
    {
        public static MemoryStream RenderToExcel(DataTable table)
        {
            MemoryStream ms = new MemoryStream();
            using (table)
            {
                IWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();
                IRow headerRow = sheet.CreateRow(0);

                // handling header.
                foreach (DataColumn column in table.Columns)
                    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value

                // handling value.
                int rowIndex = 1;

                foreach (DataRow row in table.Rows)
                {
                    IRow dataRow = sheet.CreateRow(rowIndex);

                    foreach (DataColumn column in table.Columns)
                    {
                        if (row[column] !=DBNull.Value && column.DataType == typeof(decimal))
                        {
                            dataRow.CreateCell(column.Ordinal).SetCellValue(Convert.ToDouble(row[column]));
                        }
                        else if (row[column] != DBNull.Value && column.DataType == typeof(Int32))
                        {
                            dataRow.CreateCell(column.Ordinal).SetCellValue(Convert.ToInt32(row[column]));
                        }
                        else
                        {
                            dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                        }
                    }

                    rowIndex++;
                }
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
            }
            return ms;
        }
        public static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName)
        {
            if (context.Request.Browser.Browser == "IE")
            {
                //fileName = HttpUtility.UrlEncode(fileName);
            }
            context.Response.ContentType = "application/vnd.ms-excel";
            context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
            context.Response.BinaryWrite(ms.ToArray());
        }
        static DataSet langSet = null;
        public static string GetFiledText(string code)
        {
            string rt = code;
            LoadLangSet();
            if (langSet != null && langSet.Tables.Contains("Filed"))
            {
                DataTable filedTable = langSet.Tables["Filed"];
                DataRow[] selRows = filedTable.Select("Code='"+code+"'");
                if (selRows.Length > 0)
                {
                    rt = selRows[0]["Text"].ToString();
                }
            }
            return rt;
        }
        public static string GetWorkModeText(string code)
        {
            string rt = code;
            LoadLangSet();
            if (langSet != null && langSet.Tables.Contains("WorkMode"))
            {
                DataTable workModeTable = langSet.Tables["WorkMode"];
                DataRow[] selRows = workModeTable.Select("Code='"+code+"'");
                if (selRows.Length > 0)
                {
                    rt = selRows[0]["Text"].ToString();
                }
            }
            return rt;
        }
        private static void LoadLangSet()
        {
            if (langSet == null)
            {
                langSet = new DataSet("Lang");
                langSet.ReadXml(HttpContext.Current.Server.MapPath("~/") + "\\lang.xml");
            }
        }
        public static string GetSpecShortName(object name)
        {
            string rt = string.Empty;
            if (name != DBNull.Value)
            {
                DataRow specRow = DeviceSpecService.GetByName(name.ToString());
                if (specRow != null)
                {
                    rt = specRow["SPEC_SHORTNAME"].ToString();
                }
            }
            return rt;
        }
        public static string GetSupShortName(object name)
        {
            string rt = string.Empty;
            if (name != DBNull.Value)
            {
                DataRow supRow = DeviceSupplierService.GetByName(name.ToString());
                if (supRow != null)
                {
                    rt = supRow["SUPPLIER_SHORTNAME"].ToString();
                }
            }
            return rt; 
        }
    }
}